from pandac.PandaModules import OrthographicLens
from panda3d.core import Point2,Point3,Vec3,Vec4
import copy
'''
	Andrew Oberlin, Michael Jacobs, Tom Williams
	Date: February 8, 2012
	
	Class for controlling the camera
'''
class CameraController:
	CAMERA_SPEED = 200
	'''
		Constructor for the Camera Controller
		
		Params: parent -- holds the information necessary to change the camera
	'''
	def __init__(self, parent):
		self.parent = parent
		
		props = self.parent.base.win.getProperties()
		tmp = copy.deepcopy(props)
		tmp.setSize(self.parent.WIN_WIDTH, self.parent.WIN_HEIGHT)
		self.parent.base.win.requestProperties(tmp)
		
		lens = OrthographicLens()
		lens.setFilmSize(self.parent.WIN_WIDTH, self.parent.WIN_HEIGHT)
		self.parent.base.cam.node().setLens(lens)
		self.parent.base.disableMouse()
		self.parent.camera.setPosHpr(Vec3(self.parent.WIN_WIDTH/2,self.parent.WIN_HEIGHT/2,50), Vec3(0, -90, 0))
		
		# setup watching movement
		self.prevTime = 0
		self.followTask = self.parent.taskMgr.add(self.followMario, "camera_follow")
	
	
	'''
		A task that monitors Mario and makes the camera follow him
	'''
	def followMario(self, task):
		deltaT = task.time - self.prevTime
		self.prevTime = task.time
		if (not self.parent.level.isHidden):
			marioX = self.parent.level.mario.model.getX()
			cameraX = self.parent.camera.getX()
			direction = 0
			
			if (cameraX >= self.parent.WIN_WIDTH/2.0):
				if (marioX < cameraX - self.parent.WIN_WIDTH/4.0):
					direction = -1
				elif (marioX > cameraX + self.parent.WIN_WIDTH/4.0):
					if (cameraX < self.parent.level.width - self.parent.WIN_WIDTH/2.0):
						direction = 1
			elif (cameraX < self.parent.level.width - self.parent.WIN_WIDTH/2.0):
				if (marioX > cameraX + self.parent.WIN_WIDTH/4.0):
					direction = 1
					
			self.parent.camera.setX(cameraX + direction*self.CAMERA_SPEED*deltaT)
		else:
			self.parent.camera.setPos(Vec3(self.parent.WIN_WIDTH/2.0,self.parent.WIN_HEIGHT/2.0,50))
	
		return task.cont